package starlingui.component.drag
{
	import com.imcrl.component.drager.AbstractDragger;
	import com.imcrl.component.drager.DragManager;
	import com.imcrl.component.drager.IDragListener;
	import com.imcrl.component.drager.IDraggingImage;
	import com.imcrl.core.IPoolable;
	import com.imcrl.event.DragAndDropEvent;
	
	import starling.display.MovieClip;
	import starling.textures.Texture;
	
	import starlingui.manager.TextureResource;
	
	/**
	 *  物品槽,可设置拖动
	 * @author crl
	 * 
	 */	
	public class AbsSlot extends AbstractDragger
	{		
		public static var draggerImage:IDraggingImage=new AbsDraggerImage();
		
		protected var icon:AbsIcon;
		
		protected var _skin:MovieClip;
		
		/**
		 * 外部检测; 
		 */		
		public var dragDropCheckHandle:Function;
		public var dragDropHandle:Function;
		
		protected var decorators:Vector.<AbsDecorator>;
		
		public function AbsSlot()
		{
			super();
			
			decorators=new Vector.<AbsDecorator>();
			
			addEventListener(DragAndDropEvent.DRAG_ENTER, enterHandler);
			addEventListener(DragAndDropEvent.DRAG_EXIT, exitHandler);
			
			addEventListener(DragAndDropEvent.DRAG_RECOGNIZED, start);
			addEventListener(DragAndDropEvent.DRAG_DROP, dropHandler);
			
			icon=getIconInstance();
			this.addChild(icon);
		}
		
		/**
		 * 子类重写取得图标的具体实例 
		 * @return 
		 * 
		 */		
		protected function getIconInstance():AbsIcon{
			return new AbsIcon(TextureResource.getEmpty(118,120));
		}
		
		public function set skinByClass(cls:Class):void{
			skin=new cls();
		}
		
		
		public function set skin(value:MovieClip):void{
			if(_skin){
				this.removeChild(_skin);
			}
			this._skin=value;
			if(_skin){
				this.addChildAt(_skin,0);
			}
			
			for each(var item:AbsDecorator in decorators){
				item.invalidate();
			}
		}
		
		public function addDecorator(value:AbsDecorator):Boolean{
			
			if(decorators.indexOf(value) !=-1)return false;
			
			value.slot=this;
			decorators.push(value);
			
			return true;
		}
		
		public function removeDecorator(value:AbsDecorator):Boolean{
			var index:int=decorators.indexOf(value);
			
			if(index==-1)return false;
			
			value.slot=null;
			decorators.splice(index,1);
			
			return true;
		}
		
		
		/**
		 * 开始拖动; 
		 * @param event
		 * 
		 */		
		protected function start(event:DragAndDropEvent):void
		{
			if (data == null){
				return;
			}
			
			DragManager.startDrag(this, data,event.touchEvent,draggingImage, dragListener);
		}
		
		private var _dragListener:IDragListener;
		public function get dragListener():IDragListener{
			return _dragListener;
		}
		
		public function set dragListener(value:IDragListener):void{
			_dragListener=value;
		}
		
		protected function get draggingImage():IDraggingImage{
			return draggerImage;
		}
		
		/**
		 * 
		 * @param event
		 * 
		 */		
		protected function enterHandler(event:DragAndDropEvent):void
		{
			var dragvo:* = event.sourceData;
			if (data && dragvo == data){
				return;
			}
			
			//内部基本检查;
			var dragger:AbstractDragger = event.dragInitiator;
			if(isDragAcceptableInitiator(dragger,dragvo)==false){
				return;
			}
			
			//外部特殊检查;
			if(dragDropCheckHandle !=null){
				
				if(dragDropCheckHandle(event)==false){
					return;
				}
				
			}
			
			
			gotoAndStop(2);
			DragManager.acceptDragDrop(this);
		}
		
		protected function exitHandler(event:DragAndDropEvent):void
		{
			gotoAndStop(1);
		}
		
		protected function dropHandler(event:DragAndDropEvent):void
		{
			gotoAndStop(1);
			if(dragDropHandle !=null ){
				dragDropHandle(event);
			}
			
		}
		
		public function get skin():MovieClip{
			return _skin;
		}
		
		/**
		 * 只做tooltip 及图片的基础显示; 
		 * @param value
		 * 
		 */		
		override public function set data(value:*):void{
			
			var isNewData:Boolean=_data != value;
			
			_data=value;
			
			if(isNewData){
				gotoAndStop(1);
				update(_data);
			}
			
			updateDecorator(value,isNewData)
		}
		
		/**
		 * 图标加载 
		 * @param value
		 * 
		 */		
		protected function update(value:*):void{
			
		}
		
		/**
		 * 更新装饰 
		 * @param value
		 * 
		 */		
		protected function updateDecorator(value:*,isNewData:Boolean):void{ 
			for each(var item:AbsDecorator in decorators){
				if(item.isforce || isNewData)item.data=value;
			}
		}
		
		
		/**
		 *   当从池中取出时,做一些唤醒操作;
		 */
		public function awaken():void{
			for each(var item:AbsDecorator in decorators){
				if(item is IPoolable){
					(item as IPoolable).awaken();
				}
			}
		}
		
		/**
		 * 就会被回收到池时,这时必须休眠操作;
		 */		
		public function sleep():void{
			for each(var item:AbsDecorator in decorators){
				if(item is IPoolable)(item as IPoolable).sleep();
			}
		}
		
		override public function dispose():void{
			for each(var item:AbsDecorator in decorators){
				if(item is IPoolable)(item as IPoolable).dispose();
			}
			decorators=null;
			
			icon.dispose();
			icon=null;
			
			_skin=null;
			
			super.dispose();
		}
		
		/**
		 * 更改状态 
		 * @param frame
		 * 
		 */		
		protected function gotoAndStop(frame:int):void{
			//if(_skin)_skin.gotoAndStop(frame);
		}
		
		public function getIcon():AbsIcon{
			return icon;
		}
		
		
	}
}